package com.junior.fixclub.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import androidx.annotation.Nullable;

import com.junior.fixclub.R;
import com.junior.fixclub.model.DeviceInfo;
import com.junior.fixclub.view.DeviceAdapter;
import com.junior.fixclub.view.DeviceListView;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class DevicesActivity extends Activity implements DeviceListView.CallBackInterfaceData {

    private DeviceListView listview;
    private List<DeviceInfo> devicelist;
    private DeviceAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.device_list);
        initView();
    }

    public void initView() {
        listview = findViewById(R.id.device_list);
        listview.setCallBackInterface(this);
        devicelist = new ArrayList<>();
        setAdapter();
    }

    public void setAdapter() {
        List<DeviceInfo> tmplist =  new ArrayList<>();
        for (int i = 0;i < 30; i++) {
            tmplist.add(new DeviceInfo("test"+new Random().nextInt(),"test"));
        }
        devicelist.addAll(tmplist);
        Log.e("TEST_FIX1","size:"+devicelist.size());
        adapter = new DeviceAdapter(devicelist, this);
        listview.setAdapter(adapter);
    }

    public void getAdapter() {
        List<DeviceInfo> tmplist =  new ArrayList<>();
        for (int i=0;i < 30; i++) {
            tmplist.add(new DeviceInfo("test"+new Random().nextInt(),"test"));
        }
        devicelist.addAll(tmplist);
        Log.e("TEST_FIX2","size:"+devicelist.size());
    }

    public void showListview(List<DeviceInfo> list) {
        Log.e("TEST_FIX","size3:"+list.size());
        if (adapter == null) {
            adapter = new DeviceAdapter(list, this);
            listview.setAdapter(adapter);
        } else {
            adapter.onDateChange(list);
        }
    }

    @Override
    public void onLoadDate() {
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //获取最新数据，
                getAdapter();
                // 通知listview显示item*/
                showListview(devicelist);
                //通知listview加载完毕，可以隐藏提示信息
                listview.LoadComplete();
            }
        }, 3000);
    }
}
